package top.v5it.japi.ddd.example.order.event;

import cn.hutool.extra.spring.SpringUtil;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationEvent;
import org.springframework.kafka.core.KafkaTemplate;
import top.v5it.japi.ddd.example.order.configuration.EventConfiguration;

/**
 * 订单事件，创建、支付等事件
 *
 * @author zhanpu
 * @date 2023/11/17
 */
public class OrderEvent extends ApplicationEvent {

    private static final long serialVersionUID = 7310363743020612178L;

    @Getter
    private final EventType eventType;

    public OrderEvent(Object source, EventType eventType) {
        super(source);
        this.eventType = eventType;
    }

    public void sendCreateOrderMessage() {
        @SuppressWarnings("unchecked") final KafkaTemplate<String, Object> kafkaTemplate = SpringUtil.getBean(KafkaTemplate.class);
        kafkaTemplate.send(EventConfiguration.TOPIC_NAME, source);
    }

    public void sendPaymentOrderMessage() {
        @SuppressWarnings("unchecked") final KafkaTemplate<String, Object> kafkaTemplate = SpringUtil.getBean(KafkaTemplate.class);
        kafkaTemplate.send(EventConfiguration.TOPIC_NAME, source);
    }

    @Getter
    @RequiredArgsConstructor
    public enum EventType {
        create(1), payment(2);
        private final Integer type;
    }
}
